<template>
  <button class="g-button woxiugail2" :class="{[`icon-${iconPosition}`]: true}" @click="click">
    <g-icon v-if="loading" class="icon loading" name="loading"></g-icon>
    <g-icon class="icon" v-if="icon && !loading" :name="icon"></g-icon>
    <div class="content">
      <slot></slot>
    </div>
  </button>
</template>

<script>
import Vue from 'vue'
import Icon from './icon'
Vue.component('g-icon', Icon)
export default {
  // props: ['icon', 'iconPosition']
  props: {
    icon: {},
    iconPosition: {
      type: String,
      default: 'left',
      validator (value) {
        return value === 'right' || value === 'left'
      }
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    click() {
      this.$emit('click')
    }
  }
}
</script>

<style lang="scss" scoped>
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.g-button {
  font-size: var(--font-size);
  height: var(--button-height);
  padding: 0 1em;
  vertical-align: middle;
  border-radius: var(--border-radius);
  border: 1px solid var(--border-color);
  background: var(--button-bg);
  display: inline-flex;
  justify-content: center;
  align-items: center;
  &:hover {
    border-color: var(--border-color-hover);
  }
  &:active {
    background-color: var(--button-active-bg);
  }
  &:focus{
    outline: none;
  }
  > .content{
    order: 2;
  }
  > .icon{
    order: 1;
    margin-right: .1em;
  }

  &.icon-right{
    > .icon{
      order: 2;
      margin-left: .1em;
      margin-right: 0em;
    }
    > .content{
      order: 1;
    }
  }
  .loading{
    animation: spin 2s infinite linear;
  }
}
</style>
